package com.yxkj.syh.app.huarong.activities.orders;

import android.os.Bundle;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;

import com.gyf.immersionbar.ImmersionBar;
import com.syh.app.basic.base.BaseFragment;
import com.syh.app.basic.bean.LiveDataEvent;
import com.syh.app.basic.config.BasicConstants;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.activities.orders.list.OrderListFragment;
import com.yxkj.syh.app.huarong.constants.OrderStatus;
import com.yxkj.syh.app.huarong.databinding.FragmentOrdersBinding;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * 订单
 */
public class OrdersFragment extends BaseFragment<FragmentOrdersBinding, OrdersVM> {

    String[] titles = new String[]{"全部", "待发货", "待收货", "待定价", "待核算", "待结算", "已完成", "已取消", "已退货"};
    int[] status = new int[]{OrderStatus.ALL, OrderStatus.WAIT, OrderStatus.WAIT_ECEIVING, OrderStatus.WAIT_PRICING,
            OrderStatus.WAIT_ADJUST, OrderStatus.WAIT_PAYMENT, OrderStatus.COMPLETED, OrderStatus.CANCELED, OrderStatus.RETURN};

    public OrdersFragment() {
        // Required empty public constructor
    }

    public static OrdersFragment newInstance() {
        OrdersFragment fragment = new OrdersFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.ordersVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_orders;
    }

    @Override
    protected void initView() {
        mVDBinding.statusBarHolder.getLayoutParams().height = ImmersionBar.getStatusBarHeight(getActivity());

        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            fragments.add(OrderListFragment.newInstance(status[i]));
        }
        mVDBinding.vpOrders.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                super.destroyItem(container, position, object);
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {

                return titles[position];
            }
        });
        mVDBinding.vpOrders.setOffscreenPageLimit(1);
        mVDBinding.tbStatus.setupWithViewPager(mVDBinding.vpOrders);

        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LiveDataEvent liveDataEvent) {
        switch (liveDataEvent.what) {
            case BasicConstants
                    .EVENT_SELECT_ORDER_ALL:
                requestedSelectAll = true;
                break;
        }
    }

    boolean requestedSelectAll = false;

    @Override
    protected void visibleAgain() {
        if (requestedSelectAll) {
            requestedSelectAll = false;
            if (mVDBinding.tbStatus.getSelectedTabPosition() > 0) {
                mVDBinding.tbStatus.getTabAt(0).select();
            }
        }
    }

    @Override
    public void onDestroyView() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroyView();
    }

}
